<?php

declare(strict_types=1);

namespace app\validate\system;

use think\Validate;

class SysMenuValidate extends Validate
{
    protected $rule = [
        'id' => 'integer|gt:0',
        'parent_id' => 'integer|egt:0',
        'type' => 'in:menu,button,api',
        'name' => 'max:80',
        'identifier' => 'max:120',
        'path' => 'max:255',
        'component' => 'max:255',
        'redirect' => 'max:255',
        'meta' => 'array',
        'permission_code' => 'max:120',
        'client_type' => 'in:admin,seller,buyer',
        'sort' => 'integer|egt:0',
        'is_visible' => 'in:0,1',
        'status' => 'in:0,1',
    ];

    protected $message = [
        'parent_id.integer' => '父级ID必须为整数',
        'parent_id.egt' => '父级ID不能小于0',
        'type.in' => '菜单类型不正确',
        'name.require' => '名称不能为空',
        'name.max' => '名称长度不能超过80字符',
        'identifier.require' => '标识不能为空',
        'identifier.max' => '标识长度不能超过120字符',
        'path.max' => '路径长度不能超过255字符',
        'component.max' => '组件长度不能超过255字符',
        'redirect.max' => '重定向长度不能超过255字符',
        'meta.array' => 'Meta 字段必须为对象',
        'permission_code.max' => '权限编码长度不能超过120字符',
        'client_type.in' => '客户端类型不正确',
        'sort.integer' => '排序必须为整数',
        'sort.egt' => '排序不能小于0',
        'is_visible.in' => '显示状态不正确',
        'status.in' => '状态值不正确',
    ];

    protected $scene = [
        'create' => [
            'parent_id',
            'type',
            'name',
            'identifier',
            'path',
            'component',
            'redirect',
            'meta',
            'permission_code',
            'client_type',
            'sort',
            'is_visible',
            'status',
        ],
        'update' => [
            'id',
            'parent_id',
            'type',
            'name',
            'identifier',
            'path',
            'component',
            'redirect',
            'meta',
            'permission_code',
            'client_type',
            'sort',
            'is_visible',
            'status',
        ],
    ];

    protected function sceneCreate(): self
    {
        return $this->only($this->scene['create'])
            ->append('type', 'require')
            ->append('name', 'require')
            ->append('identifier', 'require')
            ->append('client_type', 'require');
    }

    protected function sceneUpdate(): self
    {
        return $this->only($this->scene['update'])
            ->append('id', 'require')
            ->append('name', 'require')
            ->append('identifier', 'require');
    }
}
